﻿using GlennQuigmire.API.Application.GDtos;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace GlennQuigmire.API.Application.Commands
{
    public class InboundOrderProductsSearchHander : IRequestHandler<InboundOrderProductsSearchCommand, ResultT<InboundOrderProductsDto>>
    {

        private readonly IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository;
        private readonly IBaseRepositorys<Product> productRepository;

        public InboundOrderProductsSearchHander(IBaseRepositorys<InboundOrderProduct> inboundOrderProductRepository, IBaseRepositorys<Product> productRepository)
        {
                this.inboundOrderProductRepository = inboundOrderProductRepository; 
                this.productRepository = productRepository;
        }


        /// <summary>
        /// 根据选择的入库单显示
        /// </summary>
        /// <param name="request">选择的入库单主键</param>
        /// <param name="cancellationToken">允许取消异步</param>
        /// <returns>返回入库单商品列表</returns>
        public Task<ResultT<InboundOrderProductsDto>> Handle(InboundOrderProductsSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultT<InboundOrderProductsDto>();

            var list = (from a in inboundOrderProductRepository.GetAll().Where(c=>c.InboundOrderId==request.InboundOrderId&&!c.DeleteDate)
                       join b in productRepository.GetAll() on a.ProductId equals b.ProductId
                       select new InboundOrderProductsDto
                       {
                           ProductId = a.ProductId,
                           ProductNumber=b.ProductNumber,
                           ProductName=b.ProductName,
                           ProductSpecification=b.ProductSpecification,
                           StockUnit= b.StockUnit == 1 ? "条" : b.StockUnit == 2 ? "张" : b.StockUnit == 3 ? "车" : b.StockUnit == 4 ? "车" : b.StockUnit == 5 ? "天" : b.StockUnit == 6 ? "小时" : b.StockUnit == 7 ? "分钟" : b.StockUnit == 8 ? "周" : b.StockUnit == 9 ? "月" : "",
                           MaxInventory=b.MaxInventory,
                           MinInventory = b.MinInventory,
                           SaveInventory=b.SaveInventory,
                           InventoryStore=b.InventoryStore,
                           CreateId=a.CreateId,
                           CreateDate=a.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                           UpdateId=a.UpdateId,
                           UpdateDate=a.UpdateDate,
                           CreateName=a.CreateName,
                           UpdateName=a.UpdateName,
                       }).ToList();
            result.Data = list;
            return Task.FromResult(result);
        }
    }
}
